#include "StdAfx.h"
#include "JClass.h"

DEFINE_JCLASS_CPP(JDisplayMode)
DEFINE_JCLASS_INIT_CPP(JDisplayMode)
DEFINE_JCLASS_FIELD_CPP(JDisplayMode,width)
DEFINE_JCLASS_FIELD_CPP(JDisplayMode,height)
DEFINE_JCLASS_FIELD_CPP(JDisplayMode,bitdepth)
DEFINE_JCLASS_FIELD_CPP(JDisplayMode,refreshrate)

DEFINE_JCLASS_CPP(JPixelFormat)
DEFINE_JCLASS_INIT_CPP(JPixelFormat)
DEFINE_JCLASS_INIT_X_CPP(JPixelFormat,0)
DEFINE_JCLASS_FIELD_CPP(JPixelFormat,nativeref)

DEFINE_JCLASS_CPP(JCursor)
DEFINE_JCLASS_INIT_CPP(JCursor)
DEFINE_JCLASS_FIELD_CPP(JCursor,nativeContext)
DEFINE_JCLASS_FIELD_CPP(JCursor,type)

DEFINE_JCLASS_CPP(JApplicationEvent)
DEFINE_JCLASS_INIT_CPP(JApplicationEvent)

DEFINE_JCLASS_CPP(JMouseEvent)
DEFINE_JCLASS_INIT_CPP(JMouseEvent)

DEFINE_JCLASS_CPP(JWindowsInputMethod)
DEFINE_JCLASS_METHOD_CPP(JWindowsInputMethod,createInputMethodEvent)

DEFINE_JCLASS_CPP(JString)
DEFINE_JCLASS_METHOD_CPP(JString,concat)

DEFINE_JCLASS_CPP(JBigInteger)
DEFINE_JCLASS_INIT_CPP(JBigInteger)
DEFINE_JCLASS_METHOD_CPP(JBigInteger,testBit)

DEFINE_JCLASS_CPP(JKeyEvent)
DEFINE_JCLASS_INIT_CPP(JKeyEvent)

DEFINE_JCLASS_CPP(JPassiveInputMethodRequests)
DEFINE_JCLASS_METHOD_CPP(JPassiveInputMethodRequests,getCompositionLocation)

DEFINE_JCLASS_CPP(JActiveInputMethodRequests)
DEFINE_JCLASS_METHOD_CPP(JActiveInputMethodRequests,getCandidateLocation)

DEFINE_JCLASS_CPP(JKeelRuntimeImpl)
DEFINE_JCLASS_FIELD_CPP(JKeelRuntimeImpl,displaymode)

JDisplayMode::operator DEVMODE()
{
	jint width=getwidth();
	jint height=getheight();
	jint bitdepth=getbitdepth();
	jint refreshrate=getrefreshrate();

	DEVMODE devout={0};
	devout.dmFields = (-(bitdepth!=-1) & DM_BITSPERPEL) | 
		(-(refreshrate!=-1) & DM_DISPLAYFREQUENCY) | 
		DM_PELSHEIGHT|DM_PELSWIDTH;
	devout.dmPelsWidth = width;
	devout.dmSize = sizeof(devout);
	devout.dmPelsHeight = height;
	devout.dmBitsPerPel = (bitdepth!=-1) ? bitdepth : 0;
	devout.dmDisplayFrequency = (refreshrate!=-1) ? refreshrate : 0;
	
	return devout;
}

void JDisplayMode::operator=(const DEVMODE& devmode)
{
	setwidth(devmode.dmPelsWidth);
	setheight(devmode.dmPelsHeight);
	setbitdepth(devmode.dmBitsPerPel);
	setrefreshrate(devmode.dmDisplayFrequency);
}

void JDisplayMode::create(JNIEnv*env, const DEVMODE& devmode )
{
	create(env,(jint)devmode.dmPanningWidth,(jint)devmode.dmPanningHeight,(jint)devmode.dmBitsPerPel,(jint)devmode.dmDisplayFrequency);
}


jobject JWindowsInputMethod::_createInputMethodEvent( JNIEnv*env,jint id, jint* clause,int clauseLen,jstring str, jstring* subStrings,int attrLen, jint* attrClause, jbyte* attr,jint commitedTextLength, jint caretPos, jint visiblePos )
{
	jintArray clauseBoundary=NULL;
	jobjectArray clauseReading=NULL;
	if(clauseLen && clause && subStrings)
	{
		clauseBoundary=env->NewIntArray(clauseLen+1);
		env->SetIntArrayRegion(clauseBoundary,0,clauseLen+1,clause);
		clauseReading=env->NewObjectArray(clauseLen,JString::getClass(env),NULL);
		for(int i=0;i<clauseLen;++i)
			env->SetObjectArrayElement(clauseReading,i,subStrings[i]);
	}

	jintArray attributeBoundary=NULL;
	jbyteArray attributeValue=NULL;
	if(attrLen && attrClause && attr)
	{
		attributeBoundary=env->NewIntArray(attrLen+1);
		env->SetIntArrayRegion(attributeBoundary,0,attrLen+1,attrClause);
		attributeValue=env->NewByteArray(attrLen);
		env->SetByteArrayRegion(attributeValue,0,attrLen,attr);
	}

	return createInputMethodEvent(env,id,str,clauseBoundary,clauseReading,attributeBoundary,attributeValue,commitedTextLength,caretPos,visiblePos);
}


void JPixelFormat::createEnum( JNIEnv *env,HWND hWnd,HDC hDc,int &i )
{
	PIXELFORMATDESCRIPTOR ppfd;
	while(ZeroMemory(&ppfd,sizeof(ppfd)),
		ppfd.nSize = sizeof(ppfd),
		ppfd.nVersion = 1,
		DescribePixelFormat(hDc, ++i, sizeof(ppfd), &ppfd))
	{
		if((!(ppfd.dwFlags & PFD_GENERIC_FORMAT) || (ppfd.dwFlags & PFD_GENERIC_ACCELERATED))
			&& !(ppfd.dwFlags & PFD_DRAW_TO_BITMAP)
			&& ppfd.dwFlags & PFD_DOUBLEBUFFER
			&& ppfd.iPixelType != 1)
		{
			create(env,(jlong)i,(jint)ppfd.cColorBits,
				(jint)ppfd.cRedBits,(jint)ppfd.cGreenBits,(jint)ppfd.cBlueBits,(jint)ppfd.cAlphaBits,
				(jint)ppfd.cDepthBits,(jint)ppfd.cAccumBits,
				(jint)ppfd.cAccumRedBits,(jint)ppfd.cAccumGreenBits,(jint)ppfd.cAccumBlueBits,(jint)ppfd.cAccumAlphaBits,
				(jint)ppfd.cStencilBits);
			return;
		}
	}
	ReleaseDC(hWnd, hDc);
}

void JPixelFormat::createNull( JNIEnv *env )
{
	create0(env,(jlong)2,(jint)16,(jint)5,(jint)5,(jint)5,(jint)5,(jint)16,(jint)0,(jint)0,(jint)0,
		(jint)0,(jint)0,(jint)0,(jint)0);
}
